<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hshowRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roles" style="width: 100%">
              <!-- type:index会自动生成序号 -->
              <el-table-column label="序号" type="index" width="50" />
              <el-table-column prop="name" label="角色名称" width="180" />
              <el-table-column prop="description" label="描述" />
              <el-table-column width="300" label="操作">
                <template v-slot="{row}">
                  <el-button size="mini" type="success" @click="hAssign(row.id)">分配权限</el-button>
                  <el-button size="mini" @click="hEdit(row)">编辑</el-button>
                  <el-button size="mini" type="danger" @click="hClick(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page.sync="q.page"
                :page-sizes="[2, 3, 5, 10]"
                :page-size="q.pagesize"
                layout="sizes, prev, pager, next,jumper"
                :total="total"
                @size-change="hSizeChange"
                @current-change="hCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <el-dialog
      :title="isEdit ? '编辑' : '添加'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      @closed="hClosed"
    >
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="hClosed">取消</el-button>
          <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <el-dialog
      title="分配角色"
      width="40%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialogAssgin"
    >
      <assign-permission v-if="showDialogAssgin" :id="curId" @close="showDialogAssgin = false" />
    </el-dialog>
  </div>
</template>

<script>
import assignPermission from './assignPermission.vue'
import { addRole, delRolesById, getsettings, updataRole } from '@/api/settings'
export default {
  components: { assignPermission },
  data() {
    return {
      q: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      roles: [],
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      isEdit: false,
      showDialogAssgin: false,
      curId: ''

    }
  },
  created() {
    this.loadRolels()
  },
  methods: {
    hAssign(id) {
      this.curId = id
      this.showDialogAssgin = true
    },
    // 获取角色列表
    async loadRolels() {
      try {
        const res = await getsettings(this.q)
        // console.log(res)
        this.roles = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    },
    hSizeChange(pagesize) {
      this.q.pagesize = pagesize
      this.q.page = 1
      this.loadRolels()
    },
    hCurrentChange(page) {
      this.q.page = page
      this.loadRolels()
    },
    // 删除角色信息
    async  hClick(id) {
      try {
        const res = await delRolesById(id)
        this.$message.success(res.message)
        this.loadRolels()
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    // 角色信息
    async hSubmit() {
      // 兜底校验
      const valid = await this.$refs.roleForm.validate().catch(e => this.$message.error(e.message))
      if (!valid) return
      this.isEdit ? this.doEdit() : this.doAdd()
    },
    // 添加角色信息
    async  doAdd() {
      // 发送请求
      const res = await addRole(this.roleForm).catch(e => this.$message.error(e.message))
      // console.log(res)
      if (res.code !== 10000) return
      // console.log(res)
      // 提醒用户
      this.$message.success(res.message)
      // 关闭dialog
      this.showDialog = false
      // 如果最后已经满了 将total +1
      // 能被整除 就total +1
      if (this.total % this.q.page === 0) {
        this.total++
      }
      // 跳转到最后一页 向上取整
      this.q.page = Math.ceil(this.total / this.q.pagesize)
      // 重新获取数据
      this.loadRolels()
    },
    // 编辑角色信息
    async  doEdit() {
      // 发送请求
      const res = await updataRole(this.roleForm).catch(e => this.$message.error(e.message))
      // console.log(res)
      if (res.code !== 10000) return
      // console.log(res)
      // 提醒用户
      this.$message.success(res.message)
      // 关闭dialog
      this.showDialog = false
      // 重新获取数据
      this.loadRolels()
    },

    // 编辑角色信息
    hEdit(row) {
      this.isEdit = true
      // 显示dialog
      this.showDialog = true
      // 数据回填
      this.roleForm = { ...row }
    },
    // 添加按钮
    hshowRole() {
      this.isEdit = false
      // 显示dialog
      this.showDialog = true
    },
    hClosed() {
      this.showDialog = false
      // 重置表单校验项
      this.$refs.roleForm.clearValidate()
      // 清空表单
      this.roleForm = {
        name: '',
        description: ''
      }
    }
  }
}
</script>
